-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
testutil: support empty string args in fakecommand #244
Conversation
4a3e3e5
to
261a969
Compare
261a969
to
12acc94
Compare
FakeCommand allows unit tests to check which command-line arguments were passed, by recording a log of calls with their arguments. The following examples cannot be tested with FakeCommand: 1. shutdown -r "+0" "" 2. echo "" 3. configure --program-prefix "" The empty string argument confuses the command parser in testutil. The issue is that both the argument (\000) and command delimiter (\000\000) uses null characters, which makes the command split operation split on the first occurrence of two null chars. This is an invalid assumption for an empty string argument, which will be presented as two argument delimiters, without anything in between, appearing like a command delimiter. To add support for this: - Change the command delimiter from \000\000 => \000\f\n\r (magic sequence) - Update the Calls() function to split the lines correctly. Testutil can now be used for testing the following command, including the case where msg is an empty string for the wall message: shutdown [OPTIONS...] [TIME] [WALL...] cmd := exec.Command("shutdown", "-r", fmt.Sprintf("+%d", mins), msg)
12acc94
to
641012b
Compare
This was an enhancement I did for #243 which is now closed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fine with me.
@jnsgruk I did the work to enhance the testutil library to test the changes in the now closed PR. When I closed the PR, I decided that this may be a useful enhancement to keep, so I broke this out into its own PR. |
testutil: support empty string args in fakecommand
FakeCommand allows unit tests to check which command-line arguments
were passed, by recording a log of calls with their arguments.
The following examples cannot be tested with FakeCommand:
shutdown -r "+0" ""
echo ""
configure --program-prefix ""
The empty string argument confuses the command parser in testutil.
The issue is that both the argument (\000) and command delimiter
(\000\000) uses null characters, which makes the command split
operation split on the first occurrence of two null chars. This
is an invalid assumption for an empty string argument, which will
be presented as two argument delimiters, without anything in
between, appearing like a command delimiter.
To add support for this:
Change the command delimiter from \000\000 => \000\f\n\r (magic sequence)
Update the Calls() function to split the lines correctly.
Testutil can now be used for testing the following command, including the
case where msg is an empty string for the wall message:
shutdown [OPTIONS...] [TIME] [WALL...]
cmd := exec.Command("shutdown", "-r", fmt.Sprintf("+%d", mins), msg)